﻿
/*-------------------------------------------------------------------------------------
 * Project Name             : Gems
 * Created By               : santhini
 * Created Date             : 29 Oct 2012
 * Page Function            : insert the records
 * -------------------------------------------------------------------------------------
 * Ref No           Name            Date            Reason
 * R-001            santhini        29 Oct 2012    
 * -------------------------------------------------------------------------------------
 */





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Collections;


public partial class CategoryMaster : System.Web.UI.Page
{
    private int StartRowIndex
    {
        get
        {
            if (ViewState["StartRowIndex"] != null)
                return Convert.ToInt32(ViewState["StartRowIndex"].ToString());
            else
                return 0;
        }
        set { ViewState["StartRowIndex"] = value; }

    }
    private string SortColumnName
    {
        get
        {
            if (ViewState["SortColumnName"] != null)
                return ViewState["SortColumnName"].ToString();
            else
                return null;
        }
        set { ViewState["SortColumnName"] = value; }

    }
    private string SortByValue
    {
        get
        {
            if (ViewState["SortByValue"] != null)
                return ViewState["SortByValue"].ToString();
            else
                return null;
        }
        set { ViewState["SortByValue"] = value; }

    }
    private int strRowstoDisplay
    {
        get
        {
            if (ViewState["strRowstoDisplay"] != null)
                return Convert.ToInt32(ViewState["strRowstoDisplay"].ToString());
            else
                return 0;
        }
        set { ViewState["strRowstoDisplay"] = value; }

    }

    private bool boxChanged = false;
    private int intTotalRecords = 0;


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            PagerControl.btnHandler += new usercontrol_ucPageControl.OnButtonClick(PagerControl_btnHandler);
            PagerControl.dropdownchange += new EventHandler(PagerControl_dropdownchange);
            //PagerControl.RecordsPerPage = 10;
            strRowstoDisplay = 10;

            if (!IsPostBack)
            {
                DisplayRecords();
            }
        }
        catch (Exception ex)
        {
            PagerControl.Visible = false;
        }

    }


    #region UserMethods


    public void SaveRecord()
    {



        try
        {

            Hashtable hstbl1 = new Hashtable();
            hstbl1.Add("@Code", txtcatcode.Text);
            hstbl1.Add("@Name", txtCattype.Text);
            hstbl1.Add("@Type", txtcatname.Text);
            if (ViewState["id"] != null)
            {
                hstbl1.Add("@Category_ID", Convert.ToInt32(ViewState["id"]));
            }

            Int32 intIdentity = DataAccessLayer.ExecuteCommand_RowsAffected("GemsCateogry_InsertUpdate_SP", hstbl1);
            if (intIdentity > 0)
            {
                JSAlert.Show("Insert The Record");
                ViewState["id"] = null;
                DisplayRecords();
            }
            else if (intIdentity == 0)
            {
                JSAlert.Show("User Already Exists");
            }
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }


    private void DisplayRecords()
    {


        try
        {
            Hashtable hstbl = new Hashtable();

            hstbl.Add("@pi_startrowno", Convert.ToInt32(PagerControl.CurrPageNumber) != 0 ? Convert.ToInt32(PagerControl.CurrPageNumber) : 1);
            hstbl.Add("@pi_maxrows", PagerControl.RecordsPerPage);
            hstbl.Add("@pi_sorting_column", SortColumnName);
            hstbl.Add("@pi_sorting_order", SortByValue);
            hstbl.Add("@pi_category_id", Convert.ToInt32(ViewState["id"] != null ? ViewState["id"].ToString() : "0"));
            DataSet ds = DataAccessLayer.GetDataset("GemsCateogry_Fetch_SP", hstbl);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ViewState["id"] != null)
                {

                    txtcatcode.Text = ds.Tables[1].Rows[0]["Code"].ToString();
                    txtCattype.Text = ds.Tables[1].Rows[0]["Name"].ToString();
                    txtcatname.Text = ds.Tables[1].Rows[0]["Type"].ToString();
                }
                else
                {
                    GridView1.DataSource = ds.Tables[1];
                    GridView1.DataBind();

                    intTotalRecords = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
                    PagerControl.Visible = true;
                    if (intTotalRecords == 0)
                        PagerControl.Visible = false;

                    else
                    {
                        PagerControl.Visible = true;
                        PagerControl.TotalRecords = intTotalRecords;
                        PagerControl.RecordsPerPage = strRowstoDisplay;
                    }
                    //if (GridView1.Rows.Count > 9)
                    //{
                    //    GridView1.Height = Unit.Percentage(100);
                    //}
                    //else
                    //{
                    //    GridView1.Height = Unit.Pixel(223);
                    //}
                }
            }

            else
            {
                GridView1.Visible = true;
                GridView1.Visible = false;
                GridView1.DataSource = null;
                GridView1.DataBind();
                //CommonUI.ShowStatusMessage(this, Resources.Inventory.lblNoRecordsFound, -1);
                PagerControl.Visible = false;
                PagerControl.RecordsPerPage = strRowstoDisplay;
                PagerControl.TotalRecords = intTotalRecords;
            }

        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
        //try
        //{

        //    Hashtable hstbl = new Hashtable();
        //    hstbl.Add("@Category_ID", Convert.ToInt32(ViewState["id"]));
        //    DataSet ds = DataAccessLayer.GetDataset("GemsCateogry_Fetch_SP", hstbl);

        //    if (ds.Tables[0].Rows.Count > 0)
        //    {

        //        txtcatcode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
        //        txtCattype.Text = ds.Tables[0].Rows[0]["Name"].ToString();
        //        txtcatname.Text = ds.Tables[0].Rows[0]["Type"].ToString();
        //    }
        //}
        //catch (Exception ex)
        //{
        //    JSAlert.Show(ex.Message);
        //}


    }

    public void clear()
    {
        try
        {
            if (ViewState["id"] == null)
            {

                txtcatcode.Text = string.Empty;
                txtCattype.Text = string.Empty;
                txtcatname.Text = string.Empty;
            }
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }
    #endregion

    #region Button Event


    protected void grdiview_RowCreated(object sender, GridViewRowEventArgs e)
    {
        int sortColumnIndex = 0;

        if (e.Row.RowType == DataControlRowType.Header)
            sortColumnIndex = GetSortColumnIndex();

        if (sortColumnIndex != 1)
        {
            AddSortImage(sortColumnIndex, e.Row);
        }
    }

    protected void grdiview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    if (e.Row.RowIndex == 0)
        //        e.Row.Style.Add("height", "40px");
        //}
    }

    protected int GetSortColumnIndex()
    {
        foreach (DataControlField field in GridView1.Columns)
        {
            if (field.SortExpression == GridView1.SortExpression)
                return GridView1.Columns.IndexOf(field);
            else
                return -1;
        }
        return -1;
    }

    protected void AddSortImage(int columnIndex, GridViewRow HeaderRow)
    {
        Image sortImage = new Image();

        //if (showImage) // this is a boolean variable which should be false 
        //{           //  on page load so that image wont show up initially.

        if (ViewState["sortDirection"].ToString() == "Ascending")
        {
            sortImage.ImageUrl = "~/Images/up.gif";
            sortImage.AlternateText = " Ascending Order";
        }

        else
        {
            sortImage.ImageUrl = "~/Images/down.gif";
            sortImage.AlternateText = " Descending Order";
        }

        HeaderRow.Cells[columnIndex].Controls.Add(sortImage);
        //}
    }

    protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
    {
        //ViewState["sortExpression"] = e.SortExpression;
        ViewState["SortColumnName"] = e.SortExpression;
        ViewState["SortAscending"] = e.SortDirection;
        SortColumnName = ViewState["SortColumnName"].ToString();
        SortByValue = ViewState["SortAscending"].ToString();

        StartRowIndex = 1;
        DisplayRecords();

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        try
        {
            SaveRecord();
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }
    protected void ibtDelete_Click(object sender, EventArgs e)
    {
        try
        {
            GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
            int ID = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Values["Category_ID"].ToString());
            Hashtable hstbl = new Hashtable();
            hstbl.Add("@Category_ID", ID);
            Int32 var = DataAccessLayer.ExecuteCommand_RowsAffected("GemsCateogry_Delete_SP", hstbl);
            DisplayRecords();
        }

        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }


    protected void ibtEdit_Click(object sender, EventArgs e)
    {

        try
        {
            GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
            ViewState["id"] = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Values["Category_ID"].ToString());
            DisplayRecords();

        }

        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }

    protected void btnclear_Click(object sender, EventArgs e)
    {
        try
        {

            clear();
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }
    #endregion


    #region "Paging"
    /// <summary>
    /// Name of Method                    : PagerControl_dropdownchange
    /// Description                       : This method is used to show the no of records in grid based on the dropdown selection
    /// </summary>
    void PagerControl_dropdownchange(object sender, EventArgs e)
    {
        try
        {
            //CommonUI.ClearStatusMessage(this);
            strRowstoDisplay = PagerControl.RecordsPerPage;
            StartRowIndex = 1;
            DisplayRecords();

        }
        catch (Exception ex)
        {
            //objErrorHandler.LogError(1, "ManageWarehouse", "PagerControl_dropdownchange", ex.Message.ToString(), ex.StackTrace.ToString(), Session["UserID"].ToString());
            throw ex;
        }
    }
    /// <summary>
    /// Name of Method                    : PagerControl_btnHandler
    /// Description                       : This method is used to navigate one record page to another
    /// </summary>
    void PagerControl_btnHandler()
    {
        try
        {
            //CommonUI.ClearStatusMessage(this);
            StartRowIndex = PagerControl.RowIndex;
            DisplayRecords();
        }
        catch (Exception ex)
        {
            //objErrorHandler.LogError(1, "ManageWarehouse", "PagerControl_btnHandler", ex.Message.ToString(), ex.StackTrace.ToString(), Session["UserID"].ToString());
            throw ex;
        }
    }
    #endregion
}












